﻿using System.Collections;
using System.Collections.Generic;

using Entitas;

using UnityEngine;


namespace Game
{
    /// <summary>
    /// 游戏面板响应系统
    /// </summary>
    public class GameBoardSystem : ReactiveSystem<GameEntity>, IInitializeSystem
    {

        private IGroup<GameEntity> itemsGroup;

        public GameBoardSystem(Contexts contexts) : base(contexts.game)
        {
            itemsGroup = contexts.game.GetGroup(GameMatcher.GameGameBoardItem);
        }

        /// <summary>
        /// 初始化游戏面板，生成内部元素
        /// </summary>
        public void Initialize()
        {
            var gameBoard = CreatorService.Instance.CreatGameBoard().gameGameBoard;
            for (int i = 0; i < gameBoard.column; i++)
            {
                for (int j = 0; j < gameBoard.row; j++)
                {
                    if (RandomNum())
                    {
                        CreatorService.Instance.CreatBlocker();
                    }
                    else
                    {
                        CreatorService.Instance.CreatBall(i,j);
                    }
                }
            }
        }

        /// <summary>
        /// 根据随机生成的概率，判断是否可以生成障碍
        /// </summary>
        /// <returns></returns>
        private bool RandomNum()
        {
            int num = Random.Range(0, 10);
            if (num < 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        protected override void Execute(List<GameEntity> entities)
        {
            var gameboard = entities.SingleEntity().gameGameBoard;
            foreach (GameEntity entity in entities)
            {
                //TODO:超出游戏面板范围的元素需要清除
            }
        }

        protected override bool Filter(GameEntity entity)
        {
            return entity.hasGameGameBoard;
        }

        protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
        {
            return context.CreateCollector(GameMatcher.GameGameBoard);
        }
    }
}

